--- permalink: /python/offline description: How to use Plotly offline inside IPython notebooks with Plotly Offline title: Plotly Offline for IPython Notebooks has_thumbnail: false thumbnail: /images/static-image has_thumbnail: false layout: user-guide page_type: example_index language: python --- {% raw %}

Plotly Offline

Plotly Offline brings interactive Plotly graphs to the offline (local) environment.

Instead of saving the graphs to a server, your data and graphs will remain in your local system. When you're ready to share, you can just publish them to the web with an online Plotly account or to your company's internal Plotly Enterprise.

To get started with Plotly Offline, upgrade to the 1.9.x series:

In [1]:
! pip install plotly --upgrade
Requirement already up-to-date: plotly in /anaconda/lib/python2.7/site-packages
Requirement already up-to-date: requests in /anaconda/lib/python2.7/site-packages (from plotly)
Requirement already up-to-date: six in /anaconda/lib/python2.7/site-packages (from plotly)
Requirement already up-to-date: pytz in /anaconda/lib/python2.7/site-packages (from plotly)
In [2]:
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot

print __version__ # requires version >= 1.9.0
1.9.5

You can plot your graphs from a python script from command line. On executing the script, it will open a web browser with your Plotly Graph drawn.

In [3]:
from plotly.offline import plot
from plotly.graph_objs import Scatter

plot([Scatter(x=[1, 2, 3], y=[3, 1, 6])])
Out[3]:
'file:///Users/tarun/plotly/documentation/_posts/user_guide_python/temp-plot.html'

You can also plot your graphs offline inside a Jupyter(IPython) Notebook Environment. First you need to initiate the Plotly Notebook mode as below:

In [4]:
init_notebook_mode() # run at the start of every ipython notebook to use plotly.offline
                     # this injects the plotly.js source files into the notebook
In [5]:
iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])
In [6]:
from plotly.graph_objs import *
import numpy as np
In [7]:
iplot([Box(y = np.random.randn(50), showlegend=False) for i in range(45)], show_link=False)
In [8]:
x = np.random.randn(2000)
y = np.random.randn(2000)
iplot([Histogram2dContour(x=x, y=y, contours=Contours(coloring='heatmap')),
       Scatter(x=x, y=y, mode='markers', marker=Marker(color='white', size=3, opacity=0.3))], show_link=False)
In [9]:
# Plotting with Pandas

import pandas as pd
df = pd.read_csv('https://plot.ly/~etpinard/191.csv')
df.head(1)
Out[9]:
Africa_Life Expentancy [in years] Africa_text Africa_marker.size Africa_Gross Domestic Product per Capita [in USD of the year 2000] Americas_Life Expentancy [in years] Americas_text Americas_marker.size Americas_Gross Domestic Product per Capita [in USD of the year 2000] Asia_Life Expentancy [in years] Asia_text Asia_marker.size Asia_Gross Domestic Product per Capita [in USD of the year 2000] Europe_Life Expentancy [in years] Europe_text Europe_marker.size Europe_Gross Domestic Product per Capita [in USD of the year 2000] Oceania_Life Expentancy [in years] Oceania_text Oceania_marker.size Oceania_Gross Domestic Product per Capita [in USD of the year 2000]
0 72.301 Country: Algeria <br>Life Expectancy: 72.30... 33333216 6223.367465 75.32 Country: Argentina <br>Life Expectancy: 75.... 40301927 12779.37964 43.828 Country: Afghanistan <br>Life Expectancy: 4... 31889923 974.580338 76.423 Country: Albania <br>Life Expectancy: 76.42... 3600523 5937.029526 81.235 Country: Australia <br>Life Expectancy: 81.... 20434176 34435.36744
In [10]:
iplot({
    'data': [
        Scatter(x=df[continent+'_Life Expentancy [in years]'],
                y=df[continent+'_Gross Domestic Product per Capita [in USD of the year 2000]'],
                text=df[continent+'_text'],
                marker=Marker(size=df[continent+'_marker.size'], sizemode='area', sizeref=131868,),
                mode='markers',
                name=continent) for continent in ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania']
    ],
    'layout': Layout(xaxis=XAxis(title='Life Expectancy'), yaxis=YAxis(title='GDP per Capita', type='log'))
}, show_link=False)
In [11]:
# An alternative method for Pandas plotting is with cufflinks: https://github.com/santosjorge/cufflinks
! pip install cufflinks --upgrade
Requirement already up-to-date: cufflinks in /anaconda/lib/python2.7/site-packages
Requirement already up-to-date: plotly>=1.7.6 in /anaconda/lib/python2.7/site-packages (from cufflinks)
Requirement already up-to-date: pandas in /anaconda/lib/python2.7/site-packages (from cufflinks)
Requirement already up-to-date: colorlover>=0.2 in /anaconda/lib/python2.7/site-packages (from cufflinks)
Requirement already up-to-date: requests in /anaconda/lib/python2.7/site-packages (from plotly>=1.7.6->cufflinks)
Requirement already up-to-date: six in /anaconda/lib/python2.7/site-packages (from plotly>=1.7.6->cufflinks)
Requirement already up-to-date: pytz in /anaconda/lib/python2.7/site-packages (from plotly>=1.7.6->cufflinks)
Requirement already up-to-date: python-dateutil in /anaconda/lib/python2.7/site-packages (from pandas->cufflinks)
Requirement already up-to-date: numpy>=1.7.0 in /anaconda/lib/python2.7/site-packages (from pandas->cufflinks)
In [12]:
import cufflinks as cf
In [13]:
iplot(cf.datagen.lines().iplot(asFigure=True,
                               kind='scatter',xTitle='Dates',yTitle='Returns',title='Returns'))
In [14]:
iplot(cf.datagen.heatmap(20,20).iplot(asFigure=True,
                                      kind='heatmap',colorscale='spectral',title='Cufflinks - Heatmap'))
In [15]:
import pandas as pd

df_airports = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
df_airports.head()

df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
df_flight_paths.head()

airports = [ dict(
        type = 'scattergeo',
        locationmode = 'USA-states',
        lon = df_airports['long'],
        lat = df_airports['lat'],
        hoverinfo = 'text',
        text = df_airports['airport'],
        mode = 'markers',
        marker = dict(
            size=2,
            color='rgb(255, 0, 0)',
            line = dict(
                width=3,
                color='rgba(68, 68, 68, 0)'
            )
        ))]

flight_paths = []
for i in range( len( df_flight_paths ) ):
    flight_paths.append(
        dict(
            type = 'scattergeo',
            locationmode = 'USA-states',
            lon = [ df_flight_paths['start_lon'][i], df_flight_paths['end_lon'][i] ],
            lat = [ df_flight_paths['start_lat'][i], df_flight_paths['end_lat'][i] ],
            mode = 'lines',
            line = dict(
                width = 1,
                color = 'red',
            ),
            opacity = float(df_flight_paths['cnt'][i])/float(df_flight_paths['cnt'].max()),
        )
    )

layout = dict(
        title = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)',
        showlegend = False,
        height = 800,
        geo = dict(
            scope='north america',
            projection=dict( type='azimuthal equal area' ),
            showland = True,
            landcolor = 'rgb(243, 243, 243)',
            countrycolor = 'rgb(204, 204, 204)',
        ),
    )

fig = dict( data=flight_paths + airports, layout=layout )

iplot(fig)
In [16]:
import plotly.plotly as py # all methods in plotly.plotly will communicate with a Plotly Cloud or Plotly Enterprise

# get_figure downloads a figure from plot.ly or Plotly Enterprise. 
# You need to provide credentials to download figures: https://plot.ly/python/getting-started/
fig = py.get_figure('https://plot.ly/~jackp/8715', raw=True)
iplot(fig)
{% endraw %}